package cn.ihrm.sass.controller;

import cn.common.PageResult;
import cn.common.Result;
import cn.common.ResultCode;
import cn.controller.BaseController;
import cn.exception.CommonException;
import cn.ihrm.sass.entity.system.User;
import cn.ihrm.sass.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

//解决跨域问题
@CrossOrigin

@Api(description = "用户管理", value = "UserController")
@RestController
@RequestMapping(value = "/user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    /**
     * 保存
     *
     * @param user
     * @return
     */
    @ApiOperation(value = "保存用户")
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public Result save(@RequestBody User user) {
        // 设置保存的企业id
        user.setCompanyId(companyId);
        user.setCompanyName(companyName);

        //业务操作
        userService.add(user);
        //return new Result(ResultCode.SUCCESS);
        return new Result(10000, "操作成功！", true);
    }

    /**
     * 根据id更新企业
     * 1.方法
     * 2.请求参数
     * 3.响应
     */
    @ApiOperation(value = "根据id更新用户")
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public Result update(@PathVariable(value = "id") String id, @RequestBody User user) {
        //业务操作
        user.setId(id);
        userService.update(user);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 根据id删除企业
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id删除用户")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable(value = "id") String id) {
        userService.deleteById(id);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 根据id查询企业
     *
     * @param id
     * @return
     * @throws CommonException
     */
    @ApiOperation(value = "根据id查询用户")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result findById(@PathVariable(value = "id") String id) throws CommonException {

        User user = userService.findById(id);
        Result result = new Result(ResultCode.SUCCESS);
        result.setData(user);
        return result;


        //throw new CommonException(ResultCode.UNAUTHORISE);
    }

    /**
     * 查询全部企业列表
     *
     * @return
     */
    @ApiOperation(value = "查询全部用户列表")
    @RequestMapping(value = "findAll", method = RequestMethod.GET)
    public Result findAll(int page, int size, @RequestParam() Map map) {
        //int i = 1 / 0;
        Page pageUser = userService.findAll(map, page, size);
        //返回对象
        // 构造分页信息
        PageResult pageResult = new PageResult(pageUser.getTotalElements(), pageUser.getContent());
        Result result = new Result(ResultCode.SUCCESS, pageResult);
        return result;
    }

    /**
     * 分配角色
     *
     * @param map
     * @return
     */
    @ApiOperation(value = "分配角色")
    @RequestMapping(value = "/assignRoles", method = RequestMethod.PUT)
    public Result addRole(@RequestBody Map<String,Object> map) {
        // 获取被分配的用户id
        String userId = (String) map.get("id");

        //获取到角色的id列表
        List<String> roleIds = (List<String>) map.get("roleIds");
        // 调用service 完成角色分配
        userService.addRoles(userId,roleIds);

        Result result = new Result(ResultCode.SUCCESS);
        return result;
    }
}
